<?php ob_start(); ?> 
<?php header ('Content-type: text/xml; charset=utf-8'); ?>     
<?php include "constants.inc" ?>
<?php include "classes.inc" ?>
<?php session_start(); ?>
<?php

function storeClubTeams($nrOfTeams, $clubId, $departmentNr) {



  if (!isset($nrOfTeams)) {

    $nrOfTeams = 0;

  }



  if (is_numeric($nrOfTeams) == true && isset($clubId) && is_numeric($clubId) == true

		&& isset($departmentNr) && is_numeric($departmentNr) == true) {



        $depCount = "dep".$departmentNr."Count";



        $connect = mysql_connect(DB_SERVER, DB_USER, DB_PWD);

    		mysql_select_db(DB_CATALOG);



        $result = mysql_query("SET AUTOCOMMIT=0", $connect);

	      $result = mysql_query("BEGIN", $connect);



	      $result = mysql_query("select clubname from club where club_id = ".$clubId);

        $row = mysql_fetch_assoc($result);

        $clubName = $row['clubname'];

        if ( mysql_errno() != 0 ) {

          $error = $query." -> ".mysql_error();

          return "Fout tijdens inschrijving (".$error.")";

        }



        $query = "select tournament_id from zt_tournament where isCurrent = 1";

        $result = mysql_query($query, $connect);

        $row = mysql_fetch_assoc($result);

        $tournamentId = $row['tournament_id'];

        if ( mysql_errno() != 0 ) {

          $error = $query." -> ".mysql_error();

          return "Fout tijdens inschrijving (".$error.")";

        }



    		$result = mysql_query("select max(team_nr) as team_nr from zt_team te

          join zt_tournament t on t.tournament_id = te.tournament_id

          where t.isCurrent = 1 and te.club_id = ".$clubId);

        if ( mysql_errno() != 0 ) {

          $error = $query." -> ".mysql_error();

          return "Fout tijdens inschrijving (".$error.")";

        }



        $startTeamNr = 1;

        if (mysql_num_rows($result) > 0) {

            $row = mysql_fetch_assoc($result);

            $startTeamNr = $row['team_nr'] + 1;

        }



        $query = "select s.subscriptionId from zt_subscription s

          join zt_tournament t on t.tournament_id = s.tournament_id

          where t.isCurrent = 1 and club_id = ". $clubId;

        if ( mysql_errno() != 0 ) {

          $error = $query." -> ".mysql_error();

          return "Fout tijdens inschrijving (".$error.")";

        }



        $result = mysql_query($query, $connect);

        if (mysql_num_rows($result) == 0) { // not yet subscribed

          $dep1Count = 0;

          $dep2Count = 0;

          $dep3Count = 0;

          if ($departmentNr == 1) {

            $dep1Count = $nrOfTeams;

          } else if ($departmentNr == 2) {

            $dep2Count = $nrOfTeams;

          } else if ($departmentNr == 3) {

            $dep3Count = $nrOfTeams;

          }



          $query = sprintf("insert into zt_subscription (club_id, tournament_id,

            dep1Count, dep2Count, dep3Count, wishes) values (%u, %u, %u, %u, %u, '')",

            $clubId, $tournamentId, $dep1Count, $dep2Count, $dep3Count);



          if ($result != mysql_query($query, $connect)) {

             $error = $query." -> ".mysql_error();

             $result = mysql_query("ROLLBACK", $connect);

             return "Fout tijdens inschrijving (".$error.")";

          }



        }	else {

          $row = mysql_fetch_assoc($result);

          $subscriptionId = $row['subscriptionId'];

          $query = "update zt_subscription set ".$depCount." = ".$nrOfTeams

          	." where club_id = ".$clubId." and tournament_id = ".$tournamentId;



          if ($result != mysql_query($query, $connect)) {

             $error = $query." -> ".mysql_error();

             $result = mysql_query("ROLLBACK", $connect);

             return "Fout tijdens inschrijving (".$error.")";

          }



          /*$query = sprintf("delete from zt_team where club_id = %u and tournament_id = %u

            and department_nr = %u", $clubId, $tournamentId, $departmentNr);



          if ($result != mysql_query($query, $connect)) {

             $error = $query." -> ".mysql_error();

             $result = mysql_query("ROLLBACK", $connect);

             return "Fout tijdens inschrijving (".$error.")";

          }*/



        }



        /*for ($i = $startTeamNr; $i < $startTeamNr + $nrOfTeams; $i++) {

            $query = sprintf("INSERT INTO zt_team (tournament_id, club_id, team_name, team_nr, department_nr)

                VALUES (%d, %d, '%s', %d, %d)",

                $tournamentId, $clubId, $clubName.' '.$i, $i, $departmentNr);



            if ($result != mysql_query($query, $connect)) {

               $error = $query." -> ".mysql_error();

               $result = mysql_query("ROLLBACK", $connect);

               return "Fout tijdens inschrijving (".$error.")";

            }

        }*/

        $result = mysql_query("COMMIT", $connect);



	    return '1';

    }



    return "Ongeldige waarden [teams:".$nrOfTeams.",club:".$clubId.",dep:".$departmentNr."]";

}

?>

<response>

  <method>storeClubTeams</method>

  <result><?php

    echo storeClubTeams($_GET['nrOfTeams'], $_GET['clubId'], $_GET['departmentNr'])?></result>

</response>